import wx

from pyDeminBerenice import *

la=20
lo=20
nbMines=40
X=0
Y=0

class DeminWindow(wx.Panel):
	def __init__(self, parent, ID):
		wx.Panel.__init__(self,parent,ID)
		self.SetSize(parent.GetClientSize())
		self.init_buffer()
		self.Bind(wx.EVT_PAINT,self.OnPaint)
		self.Bind(wx.EVT_LEFT_DOWN,self.OnLeft)
		self.Bind(wx.EVT_RIGHT_DOWN,self.OnRight)
		self.game=0
		self.dc=0
		

	def OnLeft(self,evt):
		(w,h)=self.GetClientSize()
		h-=23
		nx=(w-8)/la
		ny=(h-8)/lo
		x,y = evt.GetPosition()
		X=int((x-10)/nx+1.0/2)
		Y=int((y-10)/ny+1.0/2)
		#print x,X,y,Y
		self.jeuUnTour(X,Y)

	def OnRight(self,evt):
		(w,h)=self.GetClientSize()
		h-=23
		nx=(w-8)/la
		ny=(h-8)/lo
		x,y = evt.GetPosition()
		X=int((x-10)/nx+1.0/2)
		Y=int((y-10)/ny+1.0/2)
		if self.game.get_value_Dec(X,Y)=="c":
			self.game.set_value_Dec(X,Y,"!")
		elif self.game.get_value_Dec(X,Y)=="!":
			self.game.set_value_Dec(X,Y,"?")
		elif self.game.get_value_Dec(X,Y)=="?":
			self.game.set_value_Dec(X,Y,"c")
		self.draw(self.game.grilleDec)

	def OnPaint(self,evt):
		dc = wx.BufferedPaintDC(self,self.buffer)

	def init_buffer(self):
		w,h = self.GetClientSize()
		self.buffer = wx.EmptyBitmap(w,h)
		self.dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
		self.game=pyDemin(la,lo,nbMines)
		self.game.nbMinesVoisinage()
		self.draw(self.game.grilleDec)
	#	self.jeuUnTour()
		
	def draw(self,l):
		dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
		dc.Clear()
		dc.SetBrush(wx.Brush("white"))
		dc.SetPen(wx.Pen("black",1))
		(x,y)=self.GetClientSize()
		y-=23
		dc.DrawRectangle(10,10,x-17,y-17)
		nx=(x-8)/la
		ny=(y-8)/lo
		nbx=nx+10
		nby=ny+10
		for i in range(la-1):
			dc.DrawLine(nbx+nx*i,10,nbx+nx*i,y-8)
		for i in range(lo-1):
			dc.DrawLine(10,nby+ny*i,x-8,nby+ny*i)
		for i in range(len(l)):
			(x,y)=(i%la,i/la)
			xp=nx*(x+1)+10-nx/2
			yp=ny*(y+1)+10-ny/2
			#print l[i]
			if l[i]=='c':
				dc.DrawCircle(xp,yp,2)
				#print "passage c"
			elif l[i]!='d':
				dc.DrawText("%s"%l[i],xp-3,yp-3)
				#print "passage int"
			#else:
				#print "passage autre"
	

	def jeuUnTour(self,x,y):
		k=0
		#	print x,y
		k=self.game.deched(x,y)
	       	if k==-1:
	       		self.loose()
	       	elif k==-2:
			self.end()
		else:			
	       		self.draw(self.game.grilleDec)

	def loose(self):
		dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
		dc.Clear()
		dc.SetBrush(wx.Brush("white"))
		dc.SetPen(wx.Pen("black",1))
		(x,y)=self.GetClientSize()
		s="Game Over \n New Game=CTRL+O"
		dc.DrawText(s,x/2.5,y/2.5)

	def end(self):
		dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
		dc.Clear()
		dc.SetBrush(wx.Brush("white"))
		dc.SetPen(wx.Pen("black",1))
		(x,y)=self.GetClientSize()
		s="You win \n New Game=CTRL+O"
		dc.DrawText(s,x/2.5,y/2.5)


class GameFrame(wx.Frame):
	def __init__( self, parent, ID, title, pos=wx.DefaultPosition,size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
		wx.Frame.__init__(self, parent, ID, title, pos, size, style)
		
		self.SetAutoLayout(True)
		self.GameWindow = DeminWindow(self,-1)
		self.GameWindow.init_buffer()

		menubar = wx.MenuBar()

		Jeu= wx.Menu(style = wx.MENU_TEAROFF )
		Jeu.Append(wx.ID_OPEN,"&New","New Game")
		Jeu.AppendSeparator()
		Jeu.Append(wx.ID_EXIT,"&Exit","Exit")

		Help= wx.Menu(style = wx.MENU_TEAROFF )
		Help.Append(8,"&Help","Help")
		
		menubar.Append(Jeu, "&Game")
		menubar.Append(Help,"&Help")
		self.SetMenuBar(menubar)

		self.Bind(wx.EVT_MENU,self.kill,id=wx.ID_EXIT)
		self.Bind(wx.EVT_MENU,self.open,id=wx.ID_OPEN)

	def kill(self,event):
		self.Destroy()

	def open(self,event):
		self.GameWindow.init_buffer()
	
	
class GameApp(wx.App):
	def OnInit(self):
		win = GameFrame(None, -1, "This is a wx.Frame", size=(600, 600),style = wx.DEFAULT_FRAME_STYLE)
		win.Show(True)
		self.SetTopWindow(win)
		return True



app = GameApp()
app.MainLoop()
